# coding-utf-8

from qtpy import QtWidgets, QtGui, QtCore

class NavigationRadioButton(QtWidgets.QRadioButton):
    clicked = QtCore.Signal()
    def __init__(self, parent=None, text=''):
        super(NavigationRadioButton, self).__init__()
        # 
        self._frame = QtWidgets.QFrame(self)
        self._frame_layout = QtWidgets.QVBoxLayout(self._frame)
        self._frame_layout.setContentsMargins(3,3,3,6)
        self._frame_layout.setSpacing(6)
        self.__text_label = QtWidgets.QLabel(self)
        self.__text_label.setStyleSheet('border:none;background-color:rgba(0,0,0,0);')
        self.__text_label.setAlignment(QtCore.Qt.AlignCenter)
        self.__text_label.setText(text)

        # 
        _icon_main_layout = QtWidgets.QHBoxLayout()
        _icon_main_layout.setContentsMargins(0,0,0,0)
        _icon_main_layout.setSpacing(0)

        _icon_sub_layout = QtWidgets.QVBoxLayout()
        _icon_sub_layout.setContentsMargins(0,0,0,0)
        _icon_sub_layout.setSpacing(0)
        _up_spacer = QtWidgets.QSpacerItem(0, 0, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        _icon_sub_layout.addItem(_up_spacer)
        

        self.__icon = QtWidgets.QLabel(self)
        self.__icon.setScaledContents(True)
        self.__icon.setAlignment(QtCore.Qt.AlignCenter)
        self.__icon.setMaximumSize(22,22)
        self.__icon.setMinimumSize(22,22)
        self.__icon.setStyleSheet('border:none;background-color:rgba(0,0,0,0);')
        self.__icon.setHidden(1)
        _icon_sub_layout.addWidget(self.__icon)

        _left_spacer = QtWidgets.QSpacerItem(0, 0, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        _right_spacer = QtWidgets.QSpacerItem(0, 0, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)

        _icon_main_layout.addItem(_left_spacer)
        _icon_main_layout.addLayout(_icon_sub_layout)
        _icon_main_layout.addItem(_right_spacer)

        self._frame_layout.addLayout(_icon_main_layout)

        main_layout = QtWidgets.QVBoxLayout(self)
        main_layout.setContentsMargins(0,0,0,0)
        self._frame_layout.addWidget(self.__text_label)
        main_layout.addWidget(self._frame)
        self.setMinimumSize(54,54)
        self._frame.setMinimumSize(54,54)
        self._frame.setMaximumSize(54,54)
        self.setMaximumSize(54,54)
        self.toggled.connect(self.__set_style)
        self.setProperty('navigation_radio_btn', True)
        self._frame.setProperty('status', 'normal')
        self._frame.setProperty('navigation_radio_btn', True)
        self._frame.setProperty('border_radius', True)

    def setText(self, text):
        self.set_text(text)
        
    def set_checked(self, status):
        self.setChecked(status)


    def set_radius(self, size):
        self.__radius_size = size

    def set_icon(self, path):
        self.__icon.setPixmap(QtGui.QPixmap(path))
        self.__icon.setHidden(0)

    def set_text(self, text):
        self.__text_label.setText(text)

    def mouseReleaseEvent(self, e):
        self.clicked.emit()
        super(NavigationRadioButton, self).mouseReleaseEvent(e)

    def __set_style(self, status):
        if status:
            self._frame.setProperty('status', 'checked')
        else:
            self._frame.setProperty('status', 'unchecked')
        self._frame.style().polish(self._frame)
        self._frame.update()

